<?php
class Administrator_chords_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }		
	
	/*Content Management*/
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$chords_publish_date = $this->input->post('chords_publish_date');
		$song_id = $this->input->post('song_id');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');		
		$song_name = trim($this->input->post('song_name'));
		$artist_name = trim($this->input->post('artist_name'));
		$artist_sef_name = seo_url($artist_name);
		$album_name = trim($this->input->post('album_name'));
		$letters = $this->input->post('letters');
		$chords_used = $this->input->post('chords_used');
		$chords_tag = $this->input->post('chords_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$uploadify_store3 = explode(',',$this->input->post('uploadify_store3'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert chords
		$data = array(
			'song_id' => (empty($song_id) ? NULL:$song_id),
			'media_type_id' => $media_type_id,
			'chords_publish_date' => convertDatetime($chords_publish_date),
			'is_publish' => ''.$is_publish.'',			
			'letters' => ''.$letters.'',			
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('chords',$data);
		$chords_id = $this->db->insert_id();
		
		//Insert Chords Info 
		$data = array(
			'chords_id' => $chords_id,
			'song_name' => $song_name,
			'artist_name' => $artist_name,
			'artist_sef_name' => $artist_sef_name,
			'album_name' => $album_name,	
			'chords_used' => ($chords_used ? implode(':',$chords_used):NULL),
			'genre_tag' => $genre_tag,	
			'chords_tag' => $chords_tag,	
		);
		$this->db->insert('chords_info',$data);
					
		//Insert chords_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(	
				'chords_id' => 	$chords_id,		
				'chords_file_name' => $data->file_name,
				'chords_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_image',$dataImage);
			
		//Insert tabs_image
		$dataImage = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				),
				'watermark' => $this->upload_folder.'watermark/watermark_tabs.png',
				'watermark_horizontal' => 'center',
				'watermark_vertical' => 'center',
				'watermark_original' => true				
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'chords_id' => 	$chords_id,
				'tabs_file_name' => $data->file_name,
				'tabs_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_tabs_image',$dataImage);	
		
		//Insert Album image
		$dataImage = array();
		if(!empty($uploadify_store3[0])) foreach($uploadify_store3 as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage = array(			
				'album_file_name' => $data->file_name,
				'album_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('chords_info',$dataImage,array('chords_id' => $chords_id));
		
		/*Insert chords_genre*/
		$dataChordsGenre = array();
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataChordsGenre[] = array(
				'genre_id' => $value,
				'chords_id' => $chords_id
			);
		endforeach;
		if(!empty($dataChordsGenre)) $this->db->insert_batch('chords_genre',$dataChordsGenre);
		
		//Update Solr
		$this->load->library('solr');		
		$document = new Apache_Solr_Document();
		$document->id = 'chords_'.$chords_id;
		$document->chords_name_s = $song_name;
		$document->chords_image_name_s = '';	
		$document->chords_tag_s = $chords_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));	
		if(!empty($song_id)):
			$dataSongAlbum = $this->db->select('j.album_file_name,j.album_file_ext')
			->join('album_info as j','j.album_id=t.album_id','INNER')
			->get_where('song_album as t',array('t.song_id' => $song_id),1)
			->row();
			$document->chords_image_name_s = $dataSongAlbum->album_file_name.'.'.$dataSongAlbum->album_file_ext;
			$document->song_id_i = $song_id;
		else:
			if(!empty($dataImage)) $document->chords_image_name_s = $dataImage['album_file_name'].'.'.$dataImage['album_file_ext'];
		endif;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;			
		
		//Clear Memcache
		$this->memcache->delete('dataChordsThai');
		$this->memcache->delete('dataChordsInter');							
	}
	
	function content_management_edit($chords_id)
	{
		$is_publish = $this->input->post('is_publish');
		$chords_publish_date = $this->input->post('chords_publish_date');
		$song_id = $this->input->post('song_id');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');		
		$song_name = trim($this->input->post('song_name'));
		$artist_name = trim($this->input->post('artist_name'));
		$artist_sef_name = seo_url($artist_name);		
		$album_name = trim($this->input->post('album_name'));
		$letters = $this->input->post('letters');
		$chords_used = $this->input->post('chords_used');
		$chords_tag = $this->input->post('chords_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$uploadify_store3 = explode(',',$this->input->post('uploadify_store3'));
		$datetime = date('Y-m-d H:i:s');

		//Insert to chords
		$data = array(
			'song_id' => (empty($song_id) ? NULL:$song_id),
			'media_type_id' => $media_type_id,
			'chords_publish_date' => convertDatetime($chords_publish_date),
			'is_publish' => ''.$is_publish.'',			
			'letters' => ''.$letters.'',			
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('chords',$data,array('chords_id' => $chords_id));
		
		//Insert Chords Info 
		$data = array(
			'song_name' => $song_name,
			'artist_name' => $artist_name,
			'artist_sef_name' => $artist_sef_name,			
			'album_name' => $album_name,
			'chords_used' => ($chords_used ? implode(':',$chords_used):NULL),
			'genre_tag' => $genre_tag,
			'chords_tag' => $chords_tag,	
		);
		$this->db->update('chords_info',$data,array('chords_id' => $chords_id));
					
		//Insert chords_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(	
				'chords_id' => 	$chords_id,		
				'chords_file_name' => $data->file_name,
				'chords_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_image',$dataImage);
			
		//Insert tabs_image
		$dataImage = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			//Delete Image First
			$oldImages = $this->db->get_where('chords_info',array('chords_id' => $chords_id),1)->row();
			if(!empty($oldImages->tabs_file_name)):
				$map = directory_map($this->upload_folder.'chords/images/', 1);
				foreach($map as $folder):
					@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$oldImages->tabs_file_name.'.'.$oldImages->tabs_file_ext);
				endforeach;
			endif;		
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				),
				'watermark' => $this->upload_folder.'watermark/watermark_tabs.png',
				'watermark_horizontal' => 'center',
				'watermark_vertical' => 'center',
				'watermark_original' => true
			));						
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'chords_id' => 	$chords_id,
				'tabs_file_name' => $data->file_name,
				'tabs_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_tabs_image',$dataImage);
		
		//Insert Album image
		$dataImage = array();
		if(!empty($uploadify_store3[0])) foreach($uploadify_store3 as $key => $value):
			//Delete Image First
			$oldImages = $this->db->get_where('chords_info',array('chords_id' => $chords_id),1)->row();
			if(!empty($oldImages->chords_file_name)):
				$map = directory_map($this->upload_folder.'chords/images/', 1);
				foreach($map as $folder):
					@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$oldImages->album_file_name.'.'.$oldImages->album_file_ext);
				endforeach;
			endif;
			//Resize Image
			$data = imageResize(array(
				'id' => $chords_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage = array(			
				'album_file_name' => $data->file_name,
				'album_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('chords_info',$dataImage,array('chords_id' => $chords_id));
		
		/*Insert chords_genre*/
		$dataChordsGenre = array();
		$this->db->delete('chords_genre',array('chords_id' => $chords_id)); //Delete First
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataChordsGenre[] = array(
				'genre_id' => $value,
				'chords_id' => $chords_id
			);
		endforeach;
		if(!empty($dataChordsGenre)) $this->db->insert_batch('chords_genre',$dataChordsGenre);

		//Update Solr
		$this->load->library('solr');		
		$document = new Apache_Solr_Document();
		$document->id = 'chords_'.$chords_id;
		$document->chords_name_s = $song_name;
		$document->chords_tag_s = $chords_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($song_id)):
			$dataSongAlbum = $this->db->select('j.album_file_name,j.album_file_ext')
			->join('album_info as j','j.album_id=t.album_id','INNER')
			->get_where('song_album as t',array('t.song_id' => $song_id),1)
			->row();
			$document->chords_image_name_s = $dataSongAlbum->album_file_name.'.'.$dataSongAlbum->album_file_ext;
			$document->song_id_i = $song_id;
		else:
			if(!empty($dataImage)) $document->chords_image_name_s = $dataImage['album_file_name'].'.'.$dataImage['album_file_ext'];
		endif;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Clear Memcache
		$this->memcache->delete('dataChordsThai');
		$this->memcache->delete('dataChordsInter');									
	}	
	
	function ajax_delete_chords($chords_image_id)
	{
		$dataImage = $this->db->get_where('chords_image',array('chords_image_id' => $chords_image_id),1)->row();
		$this->db->delete('chords_image',array('chords_image_id' => $chords_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->chords_file_name.'.'.$dataImage->chords_file_ext);
		endforeach;
		exit;
	}
	
	function ajax_delete_tabs($tabs_image_id)
	{
		$dataImage = $this->db->get_where('chords_tabs_image',array('tabs_image_id' => $tabs_image_id),1)->row();
		$this->db->delete('chords_tabs_image',array('tabs_image_id' => $tabs_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->tabs_file_name.'.'.$dataImage->tabs_file_ext);
		endforeach;
		exit;
	}
	
	function ajax_delete_album($chords_id)
	{
		$dataImage = $this->db->get_where('chords_info',array('chords_id' => $chords_id),1)->row();
		$this->db->update('chords_info',array('album_file_name' => '','album_file_ext' => ''),array('chords_id' => $chords_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->album_file_name.'.'.$dataImage->album_file_ext);
		endforeach;
		exit;
	}	

	function ajax_update_chords_image()
	{
		$tabs_image_id = $this->input->post('chords_image_id');
		if(!empty($tabs_image_id)) foreach($tabs_image_id as $key => $value):
			$this->db->update('chords_image',array('chords_image_order' => $key),array('chords_image_id' => $value));
		endforeach;
		exit;		
	}	
	
	function ajax_update_tabs_image()
	{
		$tabs_image_id = $this->input->post('tabs_image_id');
		if(!empty($tabs_image_id)) foreach($tabs_image_id as $key => $value):
			$this->db->update('chords_tabs_image',array('tabs_image_order' => $key),array('tabs_image_id' => $value));
		endforeach;
		exit;		
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		$ids = array();
		foreach($id as $value):
			$ids[] = "chords_".$value;
			$dataImage = $this->db->get_where('chords_info',array('chords_id' => $value))->row();
			$dataChordsImage = $this->db->get_where('chords_image',array('chords_id' => $value))->result();
			$dataTabsImage = $this->db->get_where('chords_tabs_image',array('chords_id' => $value))->result();
			$tables = array('chords','chords_info','chords_click_stat','chords_image','chords_tabs_image');
			$this->db->where('chords_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->album_file_name.'.'.$dataImage->album_file_ext);
				foreach($dataChordsImage as $images):
					@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$images->chords_file_name.'.'.$images->chords_file_ext);
				endforeach;
				foreach($dataTabsImage as $images):
					@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$images->tabs_file_name.'.'.$images->tabs_file_ext);
				endforeach;				
			endforeach;					
		endforeach;
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;		
		
		//Clear Memcache
		$this->memcache->delete('dataChordsThai');
		$this->memcache->delete('dataChordsInter');				
	}
	/*End Content Management*/
	
	/*Article Management*/
	function article_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$article_publish_date = $this->input->post('article_publish_date');
		$article_subject = trim($this->input->post('article_subject'));
		$article_brief = trim($this->input->post('article_brief'));
		$article_detail = trim($this->input->post('article_detail'));
		$article_tag = $this->input->post('article_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert chords
		$data = array(
			'article_type' => 'article',
			'article_publish_date' => convertDatetime($article_publish_date),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('chords_article',$data);
		$article_id = $this->db->insert_id();
		
		//Insert Chords Info 
		$data = array(
			'article_id' => $article_id,
			'article_subject' => $article_subject,
			'article_brief' => $article_brief,
			'article_detail' => $article_detail,	
			'article_tag' => $article_tag,	
		);
		$this->db->insert('chords_article_info',$data);
		
		//Insert image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $article_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'article_id' => $article_id,						
				'article_file_name' => $data->file_name,
				'article_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_article_image',$dataImage);
			
		//Clear Memcache
		$this->memcache->delete('dataChordsArticleIndex');
		$this->memcache->delete('dataChordsArticle');
	}
	
	function article_management_edit($article_id)
	{
		$is_publish = $this->input->post('is_publish');
		$article_publish_date = $this->input->post('article_publish_date');
		$article_subject = trim($this->input->post('article_subject'));
		$article_brief = trim($this->input->post('article_brief'));
		$article_detail = trim($this->input->post('article_detail'));
		$article_tag = $this->input->post('article_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');

		//Insert to chords
		$data = array(
			'article_type' => 'article',
			'article_publish_date' => convertDatetime($article_publish_date),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('chords_article',$data,array('article_id' => $article_id));
		
		//Insert Chords Info 
		$data = array(
			'article_id' => $article_id,
			'article_subject' => $article_subject,
			'article_brief' => $article_brief,
			'article_detail' => $article_detail,	
			'article_tag' => $article_tag,	
		);
		$this->db->update('chords_article_info',$data,array('article_id' => $article_id));
		
		//Insert Album image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $article_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(	
				'article_id' => $article_id,		
				'article_file_name' => $data->file_name,
				'article_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_article_image',$dataImage);
		
		//Clear Memcache
		$this->memcache->delete('dataChordsArticleIndex');
		$this->memcache->delete('dataChordsArticle');		
	}
	
	function ajax_article_update_image()
	{
		$article_image_id = $this->input->post('article_image_id');
		if(!empty($article_image_id)) foreach($article_image_id as $key => $value):
			$this->db->update('chords_article_image',array('article_image_order' => $key),array('article_image_id' => $value));
		endforeach;
		exit;		
	}	
	
	function ajax_article_delete_image($article_image_id)
	{
		$dataImage = $this->db->get_where('chords_article_image',array('article_image_id' => $article_image_id),1)->row();
		$this->db->delete('chords_article_image',array('article_image_id' => $article_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->article_file_name.'.'.$dataImage->article_file_ext);
		endforeach;
		exit;
	}
	
	function article_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('chords_article_info',array('article_id' => $value))->result();
			$tables = array('chords_article','chords_article_info');
			$this->db->where('article_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->article_file_name.'.'.$dataImage->article_file_ext);
			endforeach;					
		endforeach;
		
		//Clear Memcache
		$this->memcache->delete('dataChordsArticle');		
	}
	/*End Article Management*/	
	
	/*Clip Management*/
	function clip_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$clip_publish_date = $this->input->post('clip_publish_date');
		$clip_subject = trim($this->input->post('clip_subject'));
		$clip_brief = trim($this->input->post('clip_brief'));
		$clip_detail = trim($this->input->post('clip_detail'));
		$clip_tag = $this->input->post('clip_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert chords
		$data = array(
			'article_type' => 'clip',
			'article_publish_date' => convertDatetime($clip_publish_date),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('chords_article',$data);
		$article_id = $this->db->insert_id();
		
		//Insert Chords Info 
		$data = array(
			'article_id' => $article_id,
			'article_subject' => $clip_subject,
			'article_brief' => $clip_brief,
			'article_detail' => $clip_detail,	
			'article_tag' => $clip_tag,	
		);
		$this->db->insert('chords_article_info',$data);
		
		//Insert image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $article_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'article_id' => $article_id,		
				'article_file_name' => $data->file_name,
				'article_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_article_image',$dataImage);
		
		//Insert clip file
		$dataFile = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			$dateY = date('Y',strtotime($datetime)).'/';
			$dateM = date('m',strtotime($datetime)).'/';
			$dateD = date('d',strtotime($datetime)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'chords/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($article_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($article_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'clip_file_name' => $file_name,
				'clip_file_ext' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('chords_article_info',$dataFile,array('article_id' => $article_id));				
	}
	
	function clip_management_edit($article_id)
	{
		$is_publish = $this->input->post('is_publish');
		$clip_publish_date = $this->input->post('clip_publish_date');
		$clip_subject = trim($this->input->post('clip_subject'));
		$clip_brief = trim($this->input->post('clip_brief'));
		$clip_detail = trim($this->input->post('clip_detail'));
		$clip_tag = $this->input->post('clip_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$datetime = date('Y-m-d H:i:s');

		//Insert to chords
		$data = array(
			'article_type' => 'clip',
			'article_publish_date' => convertDatetime($clip_publish_date),
			'is_publish' => ''.$is_publish.'',
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('chords_article',$data,array('article_id' => $article_id));
		
		//Insert Chords Info 
		$data = array(
			'article_id' => $article_id,
			'article_subject' => $clip_subject,
			'article_brief' => $clip_brief,
			'article_detail' => $clip_detail,	
			'article_tag' => $clip_tag,	
		);
		$this->db->update('chords_article_info',$data,array('article_id' => $article_id));
		
		//Insert Album image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $article_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'chords/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(	
				'article_id' => $article_id,		
				'article_file_name' => $data->file_name,
				'article_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('chords_article_image',$dataImage);
		
		//Insert video_file
		$dataFile = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			$dataInfo = $this->db->select('t.*,j.create_date')
			->join('chords_article as j','j.article_id=t.article_id','INNER')
			->get_where('chords_article_info as t',array('t.article_id' => $article_id),1)->row();
					
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'chords/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($article_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($article_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			
			//delete file		
			@unlink($this->upload_folder.'chords/files/'.$dateY.$dateM.$dateD.$dataInfo->clip_file_name.'.'.$dataInfo->clip_file_ext);
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'clip_file_name' => $file_name,
				'clip_file_ext' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('chords_article_info',$dataFile,array('article_id' => $article_id));				
	}
	
	function ajax_clip_update_image()
	{
		$article_image_id = $this->input->post('article_image_id');
		if(!empty($article_image_id)) foreach($article_image_id as $key => $value):
			$this->db->update('chords_article_image',array('article_image_order' => $key),array('article_image_id' => $value));
		endforeach;
		exit;		
	}		
	
	function ajax_clip_delete_image($article_image_id)
	{
		$dataImage = $this->db->get_where('chords_article_image',array('article_image_id' => $article_image_id),1)->row();
		$this->db->delete('chords_article_image',array('article_image_id' => $article_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->article_file_name.'.'.$dataImage->article_file_ext);
		endforeach;
	}
	
	function ajax_clip_delete_media($article_id,$field = array())
	{
		$dataInfo = $this->db
		->join('chords_article_info as j','j.article_id=t.article_id','INNER')
		->get_where('chords_article as t',array('t.article_id' => $article_id),1)
		->row();
		$this->db->update('chords_article_info',array(''.$field[0].'' => '',''.$field[1].'' => ''),array('article_id' => $article_id));
		//Delete File
		$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
		$dateM = date('m',strtotime($dataInfo->create_date)).'/';
		$dateD = date('d',strtotime($dataInfo->create_date)).'/';
		@unlink($this->upload_folder.'chords/files/'.$dateY.$dateM.$dateD.$dataInfo->{''.$field[0].''}.'.'.$dataInfo->{''.$field[1].''});
		exit;
	}	
	
	function clip_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'chords/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('chords_article_info',array('article_id' => $value))->result();
			$tables = array('chords_article','chords_article_info');
			$this->db->where('article_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				@unlink($this->upload_folder.'chords/images/'.$folder.'/'.$dataImage->clip_file_name.'.'.$dataImage->clip_file_ext);
			endforeach;					
		endforeach;;		
	}
	/*End Article Management*/		
}

?>